package com.example.spider.task;

import com.example.spider.util.HtmlUtil;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.boot.web.servlet.server.Session;

import java.util.List;
import java.util.Map;

@Getter
@Setter
@Accessors(chain = true)
public class TaskResult {

    /**
     * 原始请求
     */
    private Task task;

    private int statusCode;

    private String statusDesc;

    //----- header + 几个常用的 -----
    private Map<String, String> header;

    /**
     * 响应新带来的cookie
     */
    private transient List<Session.Cookie> newCookie;

    private transient String mimeType;

    private transient String charset;

    private transient String location;

    //------------------------

    private byte[] body;

    private transient String bodyStr;// 默认是null

    //------------------------
    private Throwable exception;

    public String getBodyStr() {
        if (bodyStr == null && body != null) {
            bodyStr = HtmlUtil.htmlByteToStr(body, charset);
        }
        return bodyStr;
    }

}
